home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / bin / trigrp / main.c < prev    next >
C/C++ Source or Header  |  1993-11-02  |  8KB  |  372 lines

  1. /*  todo:   add menu with choice of groups: now icosa,octa, tetra 
  2.  */
  3. #include <stdio.h>
  4. #include <geom.h>
  5. #include "vec4.h"
  6. #include "trigrp.h"
  7. #include <mg.h>
  8. #include <window.h>
  9. #include <appearance.h>
  10. #include <color.h>
  11.  
  12. #include <gl/gl.h>
  13. #include <gl/device.h>
  14.  
  15. extern char *getenv();
  16.  
  17. /*    Barycentric:    convert from 236 triangle coordinate system
  18.         to barycentric coordinate system:
  19.         (1,0,0,0):    pi/2
  20.         (0,1,0,0):    pi/3
  21.         (0,0,1,0):    pi/6
  22.      Input:        a point in the same coordinate system as
  23.         the global variable reftri
  24.  */
  25.     
  26. #define Barycentric(src,dst) PtTransform(ireftri[G_236],&src,&dst);    \
  27.     fprintf(stderr,"Barycentric: %f %f %f %f\n",dst.x,dst.y,dst.z,dst.w);
  28. #define PI    3.1415926
  29.  
  30. char *progname;
  31. FILE *toviewer = stdout;
  32. char cmapfile[128] = "";
  33. extern char *grpnames[];
  34. extern Geom *plist;
  35. extern Geom *eucplist;
  36. extern char *group_path;
  37. static Geom *fd;
  38. extern Geom *Make2nmFD();
  39.  
  40. int dbg = 0, batch = 0, stop = 1,active_group = 0, which = 0,
  41.         automatic = 0, newgroup = 1;
  42. int menu;
  43. WnPosition wp;
  44.  
  45. HPoint3 ppoint = {0.15, 0.4, 0.0, 1.0}, bary;
  46. static HPoint3 vel = {.005, .008, 0, 0};
  47. static Transform LineTri;
  48. extern Transform reftri[], ireftri[], gens[][3]; 
  49. extern float t236[][4];
  50.  
  51. static int initmenu();
  52.  
  53. double
  54. MyDot(p0,p1)
  55. float *p0, *p1;
  56. {
  57.     return(p0[0]*p1[0] + p0[1]*p1[1] + p0[2]*p1[2] + p0[3]*p1[3]);
  58. }
  59. void
  60. init_Lines()
  61. {
  62.     int i;
  63.     LineTri[0][0] = reftri[G_236][1][1] - reftri[G_236][2][1];
  64.     LineTri[0][1] = -reftri[G_236][1][0] + reftri[G_236][2][0];
  65.     LineTri[0][3] = reftri[G_236][1][0]*reftri[G_236][2][1] -
  66.             reftri[G_236][2][0]*reftri[G_236][1][1];
  67.     LineTri[1][0] = reftri[G_236][2][1] - reftri[G_236][0][1];
  68.     LineTri[1][1] = -reftri[G_236][2][0] + reftri[G_236][0][0];
  69.     LineTri[1][3] = reftri[G_236][2][0]*reftri[G_236][0][1] -
  70.             reftri[G_236][0][0]*reftri[G_236][2][1];
  71.     LineTri[2][0] = reftri[G_236][0][1] - reftri[G_236][1][1];
  72.     LineTri[2][1] = -reftri[G_236][0][0] + reftri[G_236][1][0];
  73.     LineTri[2][3] = reftri[G_236][0][0]*reftri[G_236][1][1] -
  74.             reftri[G_236][1][0]*reftri[G_236][0][1];
  75.     LineTri[0][2] = LineTri[1][2] = LineTri[2][2] = 0.0;
  76. }
  77.     
  78. int
  79. GetOrigin(pt)
  80. register HPoint3 *pt;
  81. {
  82.     /* simple solution now */
  83.     int x,y;
  84.     float px, py, winsize;
  85.     int xsize, ysize;
  86.     int xmid, ymid;
  87.     int reverse = 0;
  88.  
  89.     xsize = (wp.xmax - wp.xmin + 1);
  90.     ysize = (wp.ymax - wp.ymin + 1);
  91.     winsize = xsize < ysize ? xsize : ysize;
  92.  
  93.     xmid = (wp.xmin + wp.xmax) / 2;
  94.     ymid = (wp.ymin + wp.ymax) / 2;
  95.  
  96.     /* only look when the left mouse button's down */
  97.     x = getvaluator(MOUSEX);
  98.     if (x > wp.xmax  || x < wp.xmin) return(0);
  99.     px = (XMAX - XMIN) * ((x - xmid)/winsize) + (XMIN+XMAX)/2;
  100.     y = getvaluator(MOUSEY);
  101.     if (y > wp.ymax  || y < wp.ymin) return(0);
  102.     py = (YMAX - YMIN) * ((y - ymid)/winsize) + (YMIN+YMAX)/2;
  103.     if(px == pt->x && py == pt->y)
  104.     return 0;
  105.     pt->x = px;
  106.     pt->y = py;
  107.     return(1);
  108. }
  109.  
  110. static
  111. SendBegin() {
  112.     fprintf(toviewer, "(progn");
  113. }
  114.  
  115. static
  116. SendEnd() {
  117.     fprintf(toviewer, ")");
  118.     fflush(toviewer);
  119. }
  120.  
  121. static
  122. SendFD()
  123. {
  124.     fprintf(toviewer, "(read geometry { define fd ");
  125.       GeomFSave(plist, toviewer, "stdout");
  126.     fprintf(toviewer, "} )\n");
  127.     fflush(toviewer);
  128. }
  129.  
  130. static
  131. SendGroup()
  132. {
  133.     static int washyp = 0;
  134.     int needhyp;
  135.     static char *space[2] = {
  136.     "(space euclidean) (normalization trigrp on)",
  137.     "(space hyperbolic)"
  138.     };
  139.  
  140.     fprintf(toviewer,
  141.         "(geometry trigrp { = INST tlist < %s/%s.prj unit : fd })",
  142.         group_path, grpnames[active_group]);
  143.     needhyp = (active_group == 4);
  144.     if(washyp != needhyp)
  145.     fprintf(toviewer, "%s\n", space[needhyp]);
  146.     washyp = needhyp;
  147.     fflush(toviewer);
  148. }
  149.  
  150. main(argc, argv)
  151. char **argv;
  152. {
  153.      int  i, n,m, count = 1;
  154.         short val, dev;
  155.     char *term = getenv("TERM");
  156.  
  157.     progname = argv[0];
  158.     active_group = G_236;
  159.  
  160.     if (!batch)
  161.       if(term == NULL || strcmp(term, "iris-ansi") != 0) {
  162.         fprintf(stderr,"Not on an iris-ansi terminal, unsafe to use windows\n");
  163.         batch = 1;
  164.     }
  165.     init_groups();
  166.     readcmap(cmapfile);
  167.     /* and initialize polylist structure */
  168.     init_plist(active_group);
  169.     init_Lines();
  170.  
  171.     /* in interactive mode, init graphics for inputting distinguished point */
  172.     if (!batch)    {
  173.     static Color gray = {.8,.8,.8};
  174.  
  175.         foreground();
  176.     mgdevice_GL();
  177.         mgctxcreate(MG_WnSet, WN_NAME, "trigrp", WN_END,
  178.             MG_CamSet, CAM_PERSPECTIVE, 0,
  179.                 CAM_FOCUS, 1.,
  180.                 CAM_HALFYFIELD, .5, CAM_END,
  181.             MG_ApSet, AP_DO, APF_FACEDRAW|APF_EDGEDRAW,
  182.                 AP_SHADING, APF_CONSTANT, AP_END,
  183.             MG_BACKGROUND, &gray,
  184.             MG_END);
  185.  
  186.     mgreshapeviewport();
  187.         initmouse();
  188.     menu = initmenu();
  189.     PtTransform(ireftri[G_236],&ppoint,&bary);
  190.         drawframe();
  191.     fprintf(stderr,"Trigrp: use left button to drag meeting point, right button for menu.\n");
  192.     }
  193.     
  194.     i = 0;
  195.     if (!batch) {
  196.       while (1) {    /* if interactive, continue forever */
  197.         if(newgroup) {
  198.         drawframe();
  199.         fd = Make2nmFD(bary, active_group, plist);
  200.         SendBegin();
  201.             SendFD();
  202.             SendGroup();
  203.         SendEnd();
  204.         newgroup = 0;
  205.         }
  206.             if (automatic) {
  207.         ppoint.x += vel.x;
  208.         ppoint.y += vel.y;
  209.         for (i=0; i<3; ++i) {
  210.             /* is bouncing point outside triangle ? */
  211.                 if (MyDot(&ppoint, LineTri[i]) < 0)     {
  212.                 PtTransform(gens[G_236][i], &ppoint, &ppoint); 
  213.                 PtTransform(gens[G_236][i], &vel, &vel); 
  214.             i = 3;
  215.             }
  216.         }
  217.         drawframe();
  218.         PtTransform(ireftri[G_236],&ppoint,&bary);
  219.             fd = Make2nmFD(bary, active_group, plist);
  220.             SendFD();
  221.           sginap(20);
  222.         }
  223.         if (!automatic || qtest()) {
  224.               dev = qread(&val);
  225.           switch(dev)    {
  226.             case LEFTMOUSE:
  227.             case MIDDLEMOUSE:
  228.             automatic = 0;
  229.             do {
  230.                 if(GetOrigin(&ppoint)) {
  231.                 PtTransform(ireftri[G_236],&ppoint,&bary);
  232.                 fd = Make2nmFD(bary, active_group, plist);
  233.                 SendFD();
  234.                 drawframe();
  235.                 }
  236.                 sginap(20);
  237.             } while(getbutton(dev));
  238.             break;
  239.                 case KEYBD:    
  240.               switch(val)    {
  241.             case '3':
  242.             case '4':
  243.             case '5':
  244.             case '6':
  245.             case '7':
  246.                 active_group = val - '3';
  247.                 newgroup = 2;
  248.                 break;
  249.             case 'a':
  250.                 automatic = 1 - automatic;
  251.                 break;
  252.             case 'p':
  253.                 fprintf(stderr,"Barycentric: %f %f %f %f\n",bary.x,bary.y,bary.z,bary.w);
  254.                 break;
  255.  
  256.             case '\033':
  257.             case 'Q':
  258.                 gexit(); exit(1);    /* Quit */
  259.             }
  260.             break;
  261.                case MENUBUTTON:
  262.             if( val ) {
  263.               switch(val = dopup(menu)) {
  264.             case 0: 
  265.             case 1: 
  266.             case 2: 
  267.             case 3: 
  268.             case 4: 
  269.                 active_group = val;
  270.                 newgroup = 2;
  271.                 break;
  272.             case 5:
  273.             fprintf(stderr,"Barycentric: %f %f %f %f\n",bary.x,bary.y,bary.z,bary.w);
  274.                 break;
  275.             case 6: 
  276.                 automatic = 1 - automatic;
  277.                 break;
  278.             case 7: exit(1); break;    /* Quit */
  279.               }
  280.             }
  281.             break;
  282.             case REDRAW:
  283.             mgreshapeviewport();
  284.             initmouse();
  285.             drawframe();
  286.             break;
  287.             }
  288.         }
  289.         }
  290.     } else {
  291.         fd = Make2nmFD(bary, active_group, plist);
  292.         GeomFSave(fd, stdout, "stdout");
  293.     }
  294. }
  295.  
  296. initmouse()
  297. {
  298.     WnWindow *w;
  299.  
  300.     qdevice(LEFTMOUSE);
  301.     qdevice(MIDDLEMOUSE);
  302.     qdevice(MENUBUTTON);
  303.     qdevice(KEYBD);
  304.  
  305.     mgctxget(MG_WINDOW, &w);
  306.     WnGet(w, WN_CURPOS, &wp);
  307. }
  308.  
  309. #ifdef sgi
  310. #include <fmclient.h>
  311. labelframe()
  312. {
  313.     static int fminitted = 0;
  314.     static fmfonthandle font = NULL;
  315.     int i;
  316.     static struct label {
  317.     float x, y;  char str[4];
  318.     } lab[3] = {
  319.     {-.05,-.05, "p/2" },
  320.     { .505, -.05, "p/3" },
  321.     { .05, .85, "p/N" }
  322.     };
  323.  
  324.     if(!fminitted) {
  325.     fminit();
  326.     font = fmfindfont("Symbol");
  327.     if(font) fmsetfont(fmscalefont(font, 12));
  328.     }
  329.     if(font) {
  330.     pushattributes();
  331.     cpack(0);
  332.     pushmatrix();
  333.     lab[2].str[2] = active_group + '3';
  334.     for(i=0; i<3; i++) {
  335.         cmov2(lab[i].x, lab[i].y);
  336.         fmprstr(lab[i].str);
  337.     }
  338.     popmatrix();
  339.     popattributes();
  340.     }
  341. }
  342. #endif /*sgi*/
  343.  
  344. drawframe()
  345. {
  346.     Transform T;
  347.  
  348.     mgworldbegin();
  349.     TmTranslate(T, -.25, -.4, 0.);
  350.     mgtransform(T);
  351.     Make2nmFD(bary, G_236, eucplist);
  352.     GeomDraw(eucplist);
  353. #ifdef sgi
  354.     labelframe();
  355. #endif
  356.     mgworldend();
  357. }
  358.  
  359. static int
  360. initmenu()
  361. {
  362.     int pup;
  363.     long groupmenu;
  364.  
  365.  
  366.     pup = defpup("Trigrp %t");
  367.     addtopup(pup, "233 [3]%x0|234 [4]%x1|235 [5]%x2|236 [6]%x3|237 [7]%x4|PrintBary [p]%x5|Autopilot [a]%x6|Quit [Q]%x7");
  368.  
  369.     return pup;
  370. }
  371.  
  372.